package cn.pupil.design.group;

import cn.pupil.design.lang.Collection;

import java.util.*;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Description:
 * @Author: pupil
 * @Date: 2024/07/27 下午 6:21
 */
public class GroupStructure implements Collection<Employee, Link> {

    // 组织ID，也是一个组织链的头部
    private String groupId;
    // 组织名称
    private String groupName;

    // 雇员列表
    private Map<String, Employee> employeeMap = new ConcurrentHashMap<>();
    // 组织架构关系；id->list
    private Map<String, List<Link>> linkMap = new ConcurrentHashMap<>();
    // 反向关系链：toID->fromId
    private Map<String, String> invertedMap = new ConcurrentHashMap<>();

    public GroupStructure(String groupId, String groupName) {
        this.groupId = groupId;
        this.groupName = groupName;
    }

    @Override
    public boolean add(Employee employee) {
        return null != employeeMap.put(employee.getuId(), employee);
    }

    @Override
    public boolean remove(Employee employee) {
        return null != employeeMap.remove(employee.getuId());
    }

    @Override
    public boolean addLink(String key, Link link) {
        // 添加方向关系链
        invertedMap.put(link.getToId(), link.getFromId());
        // 判断id在集合中是否存在
        if (linkMap.containsKey(key)) {
            // 通过id获取list集合添加link
            return linkMap.get(key).add(link);
        } else {
            // 创建新的list集合
            List<Link> links = new LinkedList<>();
            // list集合添加link
            links.add(link);
            // 添加到linkMap中
            linkMap.put(key, links);
            return true;
        }
    }

    @Override
    public boolean removeLink(String key) {
        return null != linkMap.remove(key);
    }

    @Override
    public cn.pupil.design.lang.Iterator<Employee> iterator() {

        return new cn.pupil.design.lang.Iterator<Employee>() {

            // 记录每个节点遍历次数
            HashMap<String, Integer> keyMap = new HashMap<>();

            // 遍历的节点数
            int totalIdx = 0;
            private String fromId = groupId;  // 雇员ID，From
            private String toId = groupId;   // 雇员ID，To

            @Override
            public boolean hasNext() {
                return totalIdx < employeeMap.size();
            }

            @Override
            public Employee next() {
                List<Link> links = linkMap.get(toId);
                int cursorIdx = getCursorIdx(toId);

                // 同级节点扫描
                if (null == links) {
                    cursorIdx = getCursorIdx(fromId);
                    links = linkMap.get(fromId);
                }

                // 上级节点扫描
                while (cursorIdx > links.size() - 1) {
                    fromId = invertedMap.get(fromId);
                    cursorIdx = getCursorIdx(fromId);
                    links = linkMap.get(fromId);
                }

                // 获取节点
                Link link = links.get(cursorIdx);
                toId = link.getToId();
                fromId = link.getFromId();
                totalIdx++;

                // 返回结果
                return employeeMap.get(link.getToId());
            }

            public int getCursorIdx(String key) {
                int idx = 0;
                if (keyMap.containsKey(key)) {
                    idx = keyMap.get(key);
                    keyMap.put(key, ++idx);
                } else {
                    keyMap.put(key, idx);
                }
                return idx;
            }
        };
    }

}
